from typing import Any
from typing import Any, Dict

from sqlite3_db import Sqlite3DB


class RequestProcessor:
    def __init__(self, request):
        self.request = request
        self.error = ""
    
    def process(self) -> Dict[str, Any]:
        self.data = self.request.get('data', {})
        # print(f"收到请求 | ID: {req_id} | 命令: {cmd} | 数据: {data}")
        
        response = {
            'id': self.request['id'],
            'result': False,
            'data': None,
            'error': ''
        }
        
        match self.request['cmd']:
            case "login":
                db = Sqlite3DB()
                userinfo = db.login(self.data.get('alias'), self.data.get('pwd'))
                if userinfo:
                    response['result'] = True
                    response['data'] = {
                        'name': userinfo[0],
                        'alias': userinfo[1],
                        'team_role': userinfo[2],
                    }
                else:
                    response['result'] = False
                    response['error'] = "login failed"
            case _:
                response['result'] = False
                response['error'] = "Invalid command"
        
        return response
